package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"net/http"
	"os"
)

func main() {
	http.HandleFunc("/a", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "hello worlda")
	})

	http.HandleFunc("/mysql", RunMySQL)

	http.ListenAndServe(":5555", nil)
}

// RunMySQL run mysql command
func RunMySQL(w http.ResponseWriter, r *http.Request) {
	fmt.Println("runmysql")

	info := os.Getenv("MYSQL_CONN")
	if len(info) == 0 {
		panic("mysql conn is nil")
	}
fmt.Println(info)
	db, err := sql.Open("mysql", info+"?charset=utf8&parseTime=True&loc=Local")
	if err != nil {
		panic("connect mysql error")
	}
	defer db.Close()

	_, err = db.Exec("create table `person` (`id` int not null auto_increment,`name` varchar(255),primary key(`id`))")
	if err != nil {
		panic("create table error, ")
		fmt.Println(err.Error())
	}

	w.Write([]byte("success"))

}
